﻿using System;
using System.IO;
using System.Collections;
using System.Linq;
using Statistics;

/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System.Text.RegularExpressions;


namespace ObsoleteChecker
{
	class MainClass
	{
		public static int Main (string[] args)
		{
			try {
				if(args.Contains("-l") && args.Length == 1)
				{
					Console.WriteLine (@"This program is free software: you can redistribute it and/or modify");
					Console.WriteLine (@"it under the terms of the GNU General Public License as published by");
					Console.WriteLine (@"the Free Software Foundation, either version 3 of the License, or");
					Console.WriteLine (@"(at your option) any later version.");
					Console.WriteLine ();
					Console.WriteLine (@"This program is distributed in the hope that it will be useful,");
					Console.WriteLine (@"but WITHOUT ANY WARRANTY; without even the implied warranty of");
					Console.WriteLine (@"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
					Console.WriteLine (@"GNU General Public License for more details.");
					Console.WriteLine ();
					Console.WriteLine (@"You should have received a copy of the GNU General Public License");
					Console.WriteLine (@"along with this program.  If not, see <http://www.gnu.org/licenses/>.");
				}
				else if (args.Length != 1) {
					Console.WriteLine ("Error: Incorrect number of arguments.");
					return -1;
				} else if (!File.Exists (args [0])) {
					Console.WriteLine ("Error: File not found: " + args [0]);
					return -1;
				}
				string readIn = "";
				using(Stream stream = File.OpenRead(args[0]))
				using(StreamReader reader = new StreamReader(stream))
				{
					readIn = reader.ReadToEnd();
				}
				string[] versionDates = new Regex("\n+\\Z").Replace(readIn.Replace("\r", ""), "").Split('\n');
				if (versionDates.Length <= 2) {
					Console.WriteLine ("Error: Too little versions found in file.");
				}
				double[] versions = new double[versionDates.Length-1];
				for(int i = 1; i < versionDates.Length;i++)
				{
					versions[i-1] = DateTime.Parse(versionDates[i]).Ticks - DateTime.Parse(versionDates[i - 1]).Ticks;
				}
				DateTime obsolDate = DateTime.Parse(versionDates[versionDates.Length-1]) + TimeSpan.FromTicks((long)(Quartiles.ThirdQuartile(versions.OrderBy(a => a)) + 1.5*Quartiles.IQR(versions.OrderBy(a => a))));
				if(DateTime.UtcNow.Ticks > obsolDate.Ticks)
				{
					Console.WriteLine("WARNING: This software seems to be no longer developed as of " + obsolDate.ToString("D") + ".");
				}
				else
				{
					Console.WriteLine("This software seems to be actively developed.");
					Console.WriteLine("However, if an update is not released by " + obsolDate.ToString("D") + ",");
					Console.WriteLine("then this software will be considered discontinued.");
				}
			return 0;
			}
			catch (Exception e) {
				Console.WriteLine ("Error: " + e.GetType().ToString() + " occurred.");
				Console.WriteLine ("Description: " + e.Message);
				return -1;
			}
	}
}
}